package tzhomestudio.framework.implementation.main_service;

import tzhomestudio.framework.implementation.config_service.CliCmdLine;
import tzhomestudio.framework.implementation.config_service.FileConfig;
import tzhomestudio.framework.implementation.log_service.FileLogger;
import tzhomestudio.framework.implementation.log_service.WindowMessenger;
import tzhomestudio.framework.interfaces.config_service.ICmdLine;
import tzhomestudio.framework.interfaces.config_service.IConfig;
import tzhomestudio.framework.interfaces.log_service.ILogger;
import tzhomestudio.framework.interfaces.log_service.IWindowMessenger;

/**
 * This class contains links to all services.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class FrameworkMainService {
    // Core services
    protected static IConfig config;
    protected static ILogger logger;
    protected static IWindowMessenger windowMessenger;
    protected static ICmdLine cmdLine;

    // Mutex
    protected static final Object generalMutex = new Object();

    /**
     * Executes initial preparations.
     */
    public static void executeInitialPreparations() {
        synchronized (generalMutex) {
            // execute preparations
            logger = new FileLogger();
            FrameworkMainServiceHelper.logger = logger;

            windowMessenger = new WindowMessenger();
            config = new FileConfig();
            cmdLine = new CliCmdLine();

            // update helper
            updateHelper();
        }
    }

    public static void updateHelper() {
        synchronized (generalMutex) {
            FrameworkMainServiceHelper.logger = logger;
            FrameworkMainServiceHelper.windowMessenger = windowMessenger;
            FrameworkMainServiceHelper.config = config;
        }
    }
}
